package com.qskj.springsecurity.single.security;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.web.authentication.WebAuthenticationDetails;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author shikf
 * @version 1.0     2021-02-04 14:19
 */
public class MyWebAuthenticationDetails extends WebAuthenticationDetails {

    private String imageCode;
    private String saveKaptcha;
    private boolean imageCodeIsRight;

    // 补充用户提交的验证码和session保存的验证码
    public MyWebAuthenticationDetails(HttpServletRequest request) {
        super(request);
        this.imageCode = request.getParameter("kaptcha");
        HttpSession session = request.getSession();
        this.saveKaptcha = (String) session.getAttribute("kaptcha");
        if (!StringUtils.isEmpty(this.saveKaptcha)) {
            session.removeAttribute("kaptcha");
            if (StringUtils.equals(this.saveKaptcha, this.imageCode)) {
                this.imageCodeIsRight = true;
            }
        }

    }

    public String getImageCode() {
        return imageCode;
    }

    public void setImageCode(String imageCode) {
        this.imageCode = imageCode;
    }

    public String getSaveKaptcha() {
        return saveKaptcha;
    }

    public void setSaveKaptcha(String saveKaptcha) {
        this.saveKaptcha = saveKaptcha;
    }

    public boolean isImageCodeIsRight() {
        return imageCodeIsRight;
    }

    public void setImageCodeIsRight(boolean imageCodeIsRight) {
        this.imageCodeIsRight = imageCodeIsRight;
    }
}
